package pl.prv.polanie.client.widgets.table;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dialogs.CreateGroupDialog;
import pl.prv.polanie.client.dto.CSGroupDTO;
import pl.prv.polanie.client.dto.CSVGroupDTO;
import pl.prv.polanie.client.dto.ICSDTO;
import pl.prv.polanie.client.dto.ICSVDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.panels.lists.GroupsList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 * Created: Jun 10, 2008
 */
public class TableGroups extends Composite {
	
	private GroupsList parentGroupsList = null;
	
	private CustomTable mainCT = new CustomTable(){
		@Override
		protected void runOnClickSelectedRow(int row, int column,
				ICSVDTO csvDto, Widget w) {
			super.runOnClickSelectedRow(row, column, csvDto, w);
			
			Planner.log("TableGroups.runOnClickSelectedRow: " + "" + csvDto + "  ------- row " + row + ", col " + column);
			
			CSGroupDTO csGroupDTO = (CSGroupDTO)mainCT.getCSDtoByID(csvDto.getId());
			
			//wyswietlenie okienka do edycji
			CreateGroupDialog cld = new CreateGroupDialog(parentGroupsList, csGroupDTO); 
			cld.center();
			
			
		}
		
		@Override
		protected void runOnSelectRow(ICSVDTO csvDto) {
			super.runOnSelectRow(csvDto);
			
			Planner.log("TableGroups.runOnSelectRow: " + "wybrano wiersz id " + csvDto.getId() );

			if (parentGroupsList != null) {
				parentGroupsList.showRemoveSelectedRecordButton();
			}
		}
		
	};
	
	public TableGroups(GroupsList lecturersList) {
		
		parentGroupsList = lecturersList;
		
		mainCT.setColumnCount(3);
		initWidget(mainCT);
		
		mainCT.addHeaderColumn("name", 50);
		mainCT.addHeaderColumn("students number", 130);
		mainCT.addHeaderColumn("year group", 150);
		
		mainCT.addHeaderColumn(" ", 20);
	}
	
	
	/**
	 * Dodaje do tabeli 
	 * @param cs
	 * @param csv
	 */
	public void addRow(ICSDTO cs, ICSVDTO csv) {
		mainCT.addRow(cs, csv);
	}
	
	
	public void saveOrUpdateGroupInTable(CSGroupDTO csDto) {
		CSVGroupDTO csvL = ViewDataProvider.getCSVGroup(csDto);
		mainCT.addOrUpdateDTOInTable(csDto, csvL);
	}
	
	/**
	 * Wysyla calla do serwera usuwajacego na bazie lecturera i jesli operacja sie uda to usuwa takze z tabelki.
	 * @param csDto
	 */
	public void removeSelectedGroupFromTable() {

		final CSVGroupDTO csvL = (CSVGroupDTO)mainCT.getSelectedCSVDto();
		
		if (csvL != null) {
			final CSGroupDTO csG = (CSGroupDTO)mainCT.getCSDtoByID(csvL.getId());
			
			
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					Planner.log("TableGroups.removeSelectedGroupFromTable() FAILURE");
				}
				public void onSuccess(Boolean res) {
					Planner.log("TableGroups.removeSelectedGroupFromTable() SUCCESS -> zwrocone wynik = " + res);

					//TODO dodac do kontrolki tabelki
					if (res.booleanValue() == true) {
						if (mainCT != null) {
							mainCT.removeRow(csvL);
						}
					} else {
						
					}

					//update w cache
					Cache.removeCSGroup(csG);
					
					Planner.get().getSchedulesList().reloadBranchWithGroupSchedules();
					
				}
			};
			
			ServiceHolder.get().removeCSGroup(csG, callback);
		}
		
	}	
	
	
	protected void runOnHighlightingRow(ICSVDTO csvDto) {}

	public void highlightRowByID(long id) {
		mainCT.highlightRowByID(id);
	}
	
	public void setSelectedRowByID(long id) {
		mainCT.setSelectedRowByID(id);
	}
	
	public void removeSelectionFromRow() {
		mainCT.removeSelectionFromSelectedRow();
	}
	
	public void removeHighlightingFromRows() {
		mainCT.removeHighlightingFromRows();
	}
		
}
